package com.linxin.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linxin.reggie.common.BaseContext;
import com.linxin.reggie.common.Result;
import com.linxin.reggie.entity.ShoppingCart;
import com.linxin.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加菜品或者套餐(ShoppingCart)
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("需要添加到购物车的数据信息为: {}", shoppingCart);

        // 设置用户id, 指定当前是哪个用户的购物车数据
        // 生成用户id
        long userId = BaseContext.getCurrentId();
        // 设置用户id
        shoppingCart.setUserId(userId);

        // 查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());

        if (shoppingCart.getDishId() != null) {
            // 加入购物车的是 菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            // 加入购物车的是 套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 在购物表中获取
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);


        if (one != null) {
            // 如果已存在就在原来的基础上加1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        } else {
            // 如果不存在则添加到购物车, 数量默认为1
            shoppingCart.setNumber(1);
            // 设置购物车创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);

        }

        return Result.success(shoppingCart);
    }

    /**
     * 展示购物车
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        log.info("查看购物车!!!");

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 等值查询, 根据用户id
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 根据创建时间降序排列
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);

        return Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 等值匹配
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        // 删除数据
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);

        return Result.success("购物车清空成功!!!");
    }

    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("需要删除的菜品或者套餐为", shoppingCart);

        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 用户id等值
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 判断需要减1的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            // 菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            // 套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 获取菜品或者套餐
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        // 判断是数量减1后是否为0, 为0则删除
        int number = one.getNumber() - 1;
        if (number != 0) {
            // 菜品或者套餐还有剩余
            // 执行更新操作
            one.setNumber(number);
            shoppingCartService.updateById(one);
        } else {
            // 数量为0, 删除数据
            shoppingCartService.removeById(one);
        }

        return Result.success("商品减1成功!!!");
    }
}